package com.ggkt.common.utils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	/**
	 * 过滤fck特殊字符
	 * 
	 * @param content
	 * @param replaceTo
	 * @return
	 */
	private static final Logger logger= LoggerFactory.getLogger(StringUtil.class);
	public static String changeContent(String content, String replaceTo) {
		if (content != null && content != "") {
			if (content.startsWith("<p>")) {
				content = content.substring(3);
			}
			if (content.endsWith("</p>")) {
				content = content.substring(0, content.length() - 4);
			}
			content = content.replaceAll("(\r|\n)*$", "");
			content = content.replaceAll("\r\n", replaceTo);
			content = content.replaceAll("\n", replaceTo);
		}
		return content;
	}

	public boolean hasRedundant(String[] strs) {
		if (null == strs || strs.length < 2){
			return false;
		}
		boolean isRedundant = false;
		for (int i = strs.length - 1; i > -1; i--)
			for (int j = i - 1; j > -1; j--)
				if (null != strs[i] && strs[i].equals(strs[j])) {
					isRedundant = true;
					break;
				}
		return isRedundant;
	}

	public boolean isExisted(String string, String[] strs) {
		if (null == string || null == strs || strs.length < 1){
			return false;
		}
		boolean isOverlapped = false;
		for (int i = 0; i < strs.length; i++) {
			if (null != strs[i] && strs[i].equals(string)) {
				isOverlapped = true;
				break;
			}
		}
		return isOverlapped;
	}

	public boolean isExisted(String string, String strSource, String regex) {
		if (null == string || null == strSource || null == regex
				|| strSource.length() < 1){
			return false;
		}
		return isExisted(string, strSource.split(regex));
	}

	public String makeUnique(String string, String regex) {
		if (null == string){
			return null;
		}
		if (null == regex){
			return string;
		}
		if ("".equals(string.trim()) || "".equals(regex)){
			return string;
		}
		String result = "";
		String[] strs = string.split(regex);
		strs = removeNull(strs);
		int len = strs.length;
		boolean isRedundant[] = new boolean[len];
		for (int i = 0; i < len; i++)
			isRedundant[i] = false;
		for (int i = 0; i < len; i++) {
			if (isRedundant[i]){
				continue;
			}
			for (int j = i + 1; j < len; j++) {
				if (!isRedundant[j] && null != strs[j]
						&& strs[j].equals(strs[i])) {
					isRedundant[j] = true;
				}
			}
			if (!isRedundant[i]){
				result += strs[i] + regex;
			}
		}
		return result;
	}

	public String appendUnique(String str1, String str2, String regex) {
		if (null == str1 && null == str2){
			return null;
		}
		if (null == str1 && null != str2){
			return makeUnique(str2, regex);
		}
		if (null == str2 && null != str1){
			return makeUnique(str1, regex);
		}
		if (null == regex){
			return str1 + str2;
		}
		return makeUnique(str1 + str2, regex);
	}

	public String[] removeStrings(String[] strs1, String[] strs2) {
		if (null == strs1 || null == strs2 || strs1.length < 1
				|| strs2.length < 1){
			return strs1;
		}
		int len1 = strs1.length;
		int len2 = strs2.length;
		boolean willBeRemoved;
		List remainList = new ArrayList();
		for (int i = 0; i < len1; i++) {
			willBeRemoved = false;
			for (int j = 0; j < len2; j++)
				if (null != strs2[j] && strs2[j].equals(strs1[i])) {
					willBeRemoved = true;
				}
			if (!willBeRemoved){
				remainList.add((String) strs1[i]);
			}
		}
		return (String[]) remainList.toArray(new String[0]);
	}

	public String removeString(String str1, String str2, String regex) {
		if (null == str1 || null == str2 || "".equals(str2) || "".equals(str1)
				|| null == regex){
			return str1;
		}
		String strs[] = removeStrings(str1.split(regex), str2.split(regex));
		String result = "";
		for (int i = 0; i < strs.length; i++)
			if (null != strs[i]){
				result += strs[i] + regex;
			}
		return result;
	}

	public String[] removeNull(String[] strs) {
		if (null == strs || strs.length < 1){
			return strs;
		}
		List strList = new ArrayList();
		for (int i = 0; i < strs.length; i++) {
			if (null != strs[i] && !"".equals(strs[i].trim())) {
				strList.add((String) strs[i].trim());
			}
		}
		return (String[]) strList.toArray(new String[0]);
	}

	public static String getSetRedString(String str, List strKeyList) {
		if (str == null){
			return null;
		}
		if (strKeyList == null || strKeyList.size() < 0){
			return str;
		}
		String flag = "";
		for (int i = 0; i < strKeyList.size(); i++) {
			flag = (String) strKeyList.get(i);
			if (flag != null && !flag.equals("")) {
				if (str.indexOf(flag) >= 0) {
					str = str.replaceAll(flag, "<font color='red'>" + flag
							+ "</font>");
				}
			}
		}
		return str;
	}

	public static String getStr(String src, int length) {
		if (src == null){
			return null;
		}
		int len = length - 3;
		if (src.getBytes().length <= len){
			return src;
		}
		byte[] s = src.getBytes();
		int flag = 0;
		for (int i = 0; i < len; ++i) {
			if (s[i] < 0){
				flag++;
			}
		}
		if (flag % 2 != 0){
			len--;
		}
		byte[] d = new byte[len];
		System.arraycopy(s, 0, d, 0, len);
		String ss = new String(d) + "...";
		return ss;
	}

	public static String replaceStr(String str, String findSrc,
			String replaceSrc) {
		if (str == null) {
			return null;
		}
		int i = 0;
		if ((i = str.indexOf(findSrc, i)) >= 0) {
			char[] cSrc = str.toCharArray();
			char[] cTo = replaceSrc.toCharArray();
			int len = findSrc.length();
			StringBuffer buf = new StringBuffer(cSrc.length);
			buf.append(cSrc, 0, i).append(cTo);
			i += len;
			int j = i;
			while ((i = str.indexOf(findSrc, i)) > 0) {
				buf.append(cSrc, j, i - j).append(cTo);
				i += len;
				j = i;
			}
			buf.append(cSrc, j, cSrc.length - j);
			return buf.toString();
		}
		return str;
	}

	public static String stripHtml(String content) {
		content = content.replaceAll("&amp;", "&");
		content = content.replaceAll("&lt;", "<");
		content = content.replaceAll("&gt;", ">");
		content = content.replaceAll("&quot;", "");
		content = content.replaceAll("<p.*?>", " ");
		content = content.replaceAll("<brs*/?>", " ");
		content = content.replaceAll("&nbsp;", "");
		return content.trim();
	}

	/**
	 * @see   "判断字符串是否为null是的话转为"
	 * @param obj
	 * @return
	 */
	public static String nullToString(Object obj) {
		if (obj == null || "null".equals(obj.toString())) {
			obj = "";
		}
		return String.valueOf(obj).trim();
	}

	/**
	 * @see "判断字符串是否为" 是的话转为null
	 * @return
	 */
	public static String stringToNull(String str) {
		if (str != null && "".equals(str)) {
			str = null;
		}
		return str;
	}

	/**
	 * @see "判断字符串是否是数字"
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		if ("".equals(nullToString(str))) {
			return false;
		}
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	/**
	 * @see "字符串转换为数字 不是数字的置为0"
	 * @param str
	 * @return
	 */
	public static int string2Integer(String str) {
		if (isNumeric(str)) {
			return Integer.parseInt(str);
		} else {
			return 0;
		}
	}

	/**
	 * 字符串转换为布尔类型
	 * 
	 * @param paramString
	 * @return
	 */
	public static Boolean strToBool(String paramString) {
		if (paramString == null){
			return null;
		}
		Boolean localBoolean = null;
		try {
			localBoolean = new Boolean(paramString);
		} catch (Exception localException) {
			localBoolean = null;
		}
		return localBoolean;
	}

	// 此函数过滤用，防止SQL注入
	public static String stringFilter(String str) {
		if (StringUtil.nullToString(str).equals("")) {
			return str;
		}
		String paraValue = StringUtil.nullToString(str).replaceAll("'", "’");
		paraValue = paraValue.replaceAll("--", "－－");
		return null;
	}

	public static String sqlDealStr(String str) {
		if (str == null) {
			return str;
		}
		str = str.replaceAll("'", "");
		str = str.replaceAll(" ", "");
		str = str.replaceAll("@", "");
		str = str.replaceAll("%", "");
		str = str.replaceAll("\\+", "");
		str = str.replaceAll(";", "");
		return str;
	}
	
	/**
	 * 删除指定的HTML标签
	 * @param inputString
	 * @param html
	 * @return
	 */
	public static String htmlText(String inputString, String html) {
		if (inputString == null || html == null) {
			return inputString;
		}
		String regEx = "<[\\s]*?" + html + "[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?" + html + "[\\s]*?>";
		String regEx1 = "<[\\s]*?" + html + "[^>]*?>";
		String regEx2 = "<[\\s]*?\\/[\\s]*?" + html + "[\\s]*?>";
		
		Pattern p_html; 
		Matcher m_html;
		try {
			p_html = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
			m_html = p_html.matcher(inputString);
			inputString = m_html.replaceAll(""); 
			
			p_html = Pattern.compile(regEx1, Pattern.CASE_INSENSITIVE);
			m_html = p_html.matcher(inputString);
			inputString = m_html.replaceAll(""); 
			
			p_html = Pattern.compile(regEx2, Pattern.CASE_INSENSITIVE);
			m_html = p_html.matcher(inputString);
			inputString = m_html.replaceAll(""); 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return inputString;
	}

	public static String html2Text(String inputString, String html) {
		String regEx1 = "<[\\s]*?" + html + "[^>]*?>";
		String regEx2 = "<[\\s]*?\\/[\\s]*?" + html + ">";
		if (html == null || inputString == null) {
			return inputString;
		}
		Pattern p_html; 
		Matcher m_html;
		try {p_html = Pattern.compile(regEx1, Pattern.CASE_INSENSITIVE);
		m_html = p_html.matcher(inputString);
		inputString = m_html.replaceAll(""); 
		
		p_html = Pattern.compile(regEx2, Pattern.CASE_INSENSITIVE);
		m_html = p_html.matcher(inputString);
		inputString = m_html.replaceAll(""); 
	}catch (Exception e) {
		e.printStackTrace();
	}
	return inputString;
}

	public static String trimStr(String str)
	{
		if(str != null)
		{
			return str.trim();
		}
		else
		{
			return str;
		}
	}
	
	/**
	 * 将1.0转化为1 ，用于导出excel时获取到的数字被转化成1.0
	 * @param str
	 * @return 
	 * @return String
	 */
	public static String floatToString(String str){
		str=str.trim();
		str = str.equals("")?"":str.indexOf(".")==-1?str:str.substring(0, str.lastIndexOf("."));
		return str;
	}
	
	/**
	 * 把字符串的后空格trim掉
	 * @param str
	 * @return 
	 * @return String
	 */
	public static String trimLast(String str){
		if (str != null && !"".equals(str)) {
			String newStr =("A" + str).trim().substring(1); 
			return newStr;
		}else {
			return str;
		}
	}
	
	/**
	 * 去除html标签
	 * @param htmlStr
	 * @return
	 */
	public static String delHTMLTag(String htmlStr){
		if(htmlStr!=null && !"".equals(htmlStr)){
			String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 
			String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 
			String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 
			
			Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); 
			Matcher m_script=p_script.matcher(htmlStr); 
			htmlStr=m_script.replaceAll(""); //过滤script标签 
			
			Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); 
			Matcher m_style=p_style.matcher(htmlStr); 
			htmlStr=m_style.replaceAll(""); //过滤style标签 
			
			Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); 
			Matcher m_html=p_html.matcher(htmlStr); 
			htmlStr=m_html.replaceAll(""); //过滤html标签 
			
			return htmlStr.trim(); //返回文本字符串 
		}else{
			return "";
		}
    }
	
	/****
	 * 	判断是否为空
	 * @return
	 */
	public static String judgeEmpty(String name) {
		if(StringUtils.isBlank(name)) {
			return "";
		}
		return name;
	}
	
	public static String getPayName(int payType) {
		switch(payType){
		    case 1 :
		       return "现金";
		    case 2 :
		       return "会计卡";
		    case 3 :
		    	return "考研卡";
		    case 4 :
		    	return "赠送卡";
		    case 5 :
		    	return "职称书赠卡账户";
		    case 6 :
		    	return "浙江从业卡";
		}
		return "";
	} 
	
}
